<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\api\model;

use \think\Model;
class City extends Model
{

    public function index(){
        $shu=[];
        $img=[];
        $city=[];
        $fu=$this->where('groupid','0')->where('wxx_recommend',1)->where('deleted',0)->select();
        foreach ($fu as $v){
            $child=$this->where('groupid',$v['id'])->where('recommend',1)->where('deleted',0)->field('name,thumb as img,eng_name,id as city_id')->select();
            $city[]=
                [
                    'name'=> $v['name'],
                    'id'=>$v['id'],
                    'data'=>$child
                ];
        }
        $d=[];
        $d['show_hidden']=1;
        $d['terminal']=0;
        $img1=model('Carousel')
            ->where($d)
            ->order('img_sort asc')
            ->field('id,img_name,img_src,img_sort,img_url')
            ->select();
        if (!empty($img1)){
            foreach ($img1 as $k=>$v){
                $v['img_url']= json_decode($v['img_url']);
                $img[]=  $v;
            }
        }else{
            $img=[];
        }

        //积分配置
        $Integral= db('integral')->select();

        if ($img==true&&$city==true){
            $shu=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>[
                    'image'=>$img,
                    'city'=>$city,
                    'Integral'=>$Integral
                ]
            ];
        }else{
            $shu=[
                'code'=>1,
                'msg'=>'请求失败',
                'data'=>[]
            ];
        }

        return $shu;
    }


    public function city(){
        $city=[];
        $fu= $this->where('groupid','0')->where('wxx_recommend',1)->where('deleted',0)->select();
        foreach ($fu as $v){
            $child=$this
                ->where('groupid',$v['id'])
                ->where('deleted',0)
                ->where('wxx_recommend',1)
                ->field('name,thumb as img,eng_name,id as city_id')
                ->order("sort_weight desc")
                ->select();
            $city[]=
                [
                    'name'=> $v['name'],
                    'id'=>$v['id'],
                    'data'=>$child
                ];
        }

        return $city;
    }



    //小程序城市
    public function pindex($post){
        //分页
        $page = !empty($post['page']) ? $post['page'] : 1;
        $pageLimit = !empty($post['pagelimit']) ? $post['pagelimit'] : 100;
        if ($page==1){
            $pages=0;
        }else{
            $pages= ($page-1)*$pageLimit;
        }

        //是否为分页请求单个国家城市
        $country_id = !empty($post['country_id']) ? $post['country_id'] : 0;
        if($country_id>0){
            $city=[];
            $poiCount = $this
                ->where('groupid',$country_id)
                ->where('wxx_recommend',1)
                ->where('deleted',0)
                ->count();

            $pageCount = 1;
            if($poiCount>0){
                $pageCount = ceil($poiCount/$pageLimit);
            }

            $child=$this->where('groupid',$country_id)->where('wxx_recommend',1)
                ->where('deleted',0)
                ->field('name,wxx_thumb as img,eng_name,id as city_id')
                ->order("sort_weight desc")
                ->limit($pages,$pageLimit)  //分页
                ->select();

            $city[]=
                [
                    'data'=>$child,
                    'page'=>$page,
                    'pageLimit'=>$pageLimit,
                    'pageCount'=>$pageCount,
                ];

        }else{
            $city=[];
            $fu=$this->where('groupid','0')->where('wxx_recommend',1)->where('deleted',0)->select();
            foreach ($fu as $v){
                $poiCount = $this
                    ->where('groupid',$v['id'])
                    ->where('wxx_recommend',1)
                    ->where('deleted',0)
                    ->count();

                $pageCount = 1;
                if($poiCount>0){
                    $pageCount = ceil($poiCount/$pageLimit);
                }

                $child=$this->where('groupid',$v['id'])->where('wxx_recommend',1)
                    ->where('deleted',0)
                    ->field('name,wxx_thumb as img,eng_name,id as city_id')
                    ->order("sort_weight desc")
                    ->limit($pages,$pageLimit)  //分页
                    ->select();
                $city[]=
                    [
                        'name'=> $v['name'],
                        'id'=>$v['id'],
                        'data'=>$child,
                        'page'=>$page,
                        'pageLimit'=>$pageLimit,
                        'pageCount'=>$pageCount,
                    ];
            }
        }

        $img=[];

        //积分配置
        $Integral= db('integral')->select();

        if ($city==true){
            $shu=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>[
                    'image'=>$img,
                    'city'=>$city,
                    'Integral'=>$Integral
                ]
            ];
        }else{
            $shu=[
                'code'=>1,
                'msg'=>'请求失败',
                'data'=>[]
            ];
        }

        return $shu;
    }

}
